package com.cartoonrabbit.config.task.monitors;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cartoonrabbit.consts.ActionStatus;
import com.cartoonrabbit.consts.ColockStatus;
import com.cartoonrabbit.mapper.EarlyActionMapper;
import com.cartoonrabbit.mapper.EarlyRecordMapper;
import com.cartoonrabbit.mapper.EarlyUserMapper;
import com.cartoonrabbit.pojo.EarlyAction;
import com.cartoonrabbit.pojo.EarlyRecord;
import com.cartoonrabbit.pojo.EarlyUser;
import com.cartoonrabbit.pojo.User;
import com.cartoonrabbit.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Component
public class EarlyActionTask {

    @Autowired
    private EarlyActionMapper earlyActionMapper;
    @Autowired
    private EarlyRecordMapper earlyRecordMapper;
    @Autowired
    private EarlyUserMapper earlyUserMapper;
    @Autowired
    private IUserService userService;

    // 23:00 将当天早起打卡停止报名
    @Scheduled(cron = "0 0 23 * * ?",zone = "Asia/Shanghai")
    @Transactional
    public void sotopTodayApply() {
        // 获取当前时间
        LocalDate nowDate = LocalDate.now();
        EarlyAction earlyAction = earlyActionMapper.selectActionByTime(nowDate.toString());
        if (earlyAction!=null){
            earlyAction.setActionStatus(ActionStatus.STOPAPPLY);
            earlyActionMapper.updateById(earlyAction);
        }
    }
    // 5:00 第二天将早起打卡状态设置成正在打卡中
    @Scheduled(cron = "0 0 5 * * ?",zone = "Asia/Shanghai")
    @Transactional
    public void yesterDayActionToProgressing(){
        // 获取当前时间
        LocalDate nowDate = LocalDate.now();
        nowDate=nowDate.plusDays(-1);
        EarlyAction earlyAction = earlyActionMapper.selectActionByTime(nowDate.toString());
        if (earlyAction!=null){
            earlyAction.setActionStatus(ActionStatus.PROGRESSING);
            earlyActionMapper.updateById(earlyAction);
        }
    }
    // 8:00 将早起打卡状态设置结算中
    @Scheduled(cron = "0 0 8 * * ?",zone = "Asia/Shanghai")
    @Transactional
    public void yesterDayActionToSETTLING() {
        // 获取当前时间
        LocalDate nowDate = LocalDate.now();
        nowDate=nowDate.plusDays(-1);
        EarlyAction earlyAction = earlyActionMapper.selectActionByTime(nowDate.toString());
        if (earlyAction!=null){
            earlyAction.setActionStatus(ActionStatus.SETTLING);
            earlyActionMapper.updateById(earlyAction);
        }
    }
    // 8:10 结算
    @Scheduled(cron = "0 10 8 * * ?",zone = "Asia/Shanghai")
    @Transactional
    public void yesterDayActionToComputer() {
        // 获取当前时间
        LocalDate nowDate = LocalDate.now();
        nowDate=nowDate.plusDays(-1);
        EarlyAction earlyAction = earlyActionMapper.selectActionByTime(nowDate.toString());
        if (earlyAction!=null){// 结算
            // 获取成功打卡人数
            int successCount=0;
            // 早起之星
            String earlyStart=null;
            LocalDateTime minDateTime = LocalDateTime.now();
            // 手气之星
            String luckStart=null;
            Long maxMoney=0L;
            // 毅力之星
            String powerStart=null;
            int maxPower=0;

            List<EarlyRecord> earlyRecordList = earlyRecordMapper.selectList(new QueryWrapper<EarlyRecord>()
                    .eq("action_id", earlyAction.getActionId())
                    .eq("colock_status",ColockStatus.CLOCKED));
            successCount=earlyRecordList.size();
            earlyAction.setTotalSuccess(successCount);
            earlyAction.setTotalFail(earlyAction.getTotalPerson()-successCount);

            // 计算押金分配
            Long yajing=100L*successCount;
            // 计算未打卡的人的所有金额
            Long failMoney=earlyAction.getTotalMoney()-yajing;
            // 平台抽取20%
            failMoney=Double.valueOf(Math.floor(failMoney*0.8)).longValue();
            // 平坦金额
            Long pingtanMoney=failMoney/successCount;
            // 随机奖励的钱
            Long randomTotalMoney=failMoney-pingtanMoney*successCount;
            for (EarlyRecord earlyRecord : earlyRecordList) {
                if (minDateTime.isAfter(earlyRecord.getColcokTime())){
                    minDateTime=earlyRecord.getColcokTime();
                    earlyStart=earlyRecord.getUserId();
                }
                // 押金分配
                Long selfYajing=100L;
                Long randomMoney= RandomUtil.randomLong(0,randomTotalMoney+1);
                randomTotalMoney=randomTotalMoney-randomMoney;
                Long selfAward=pingtanMoney+randomMoney;
                // 总奖励金钱
                Long totalMoney=selfYajing+selfAward;
                earlyRecord.setColockIncome(totalMoney);
                // 手气之星
                if (maxMoney<totalMoney){
                    maxMoney=totalMoney;
                    luckStart=earlyRecord.getUserId();
                }
                EarlyUser earlyUser = earlyUserMapper.selectById(earlyRecord.getUserId());
                if (earlyUser.getContinuColock()>maxPower){
                    maxPower=earlyUser.getContinuColock();
                    powerStart=earlyUser.getUserId();
                }
                earlyUser.setTotalIncome(earlyUser.getTotalIncome()+earlyRecord.getColockIncome());
                earlyUserMapper.updateById(earlyUser);
                earlyRecordMapper.updateById(earlyRecord);
                User byId = userService.getById(earlyRecord.getUserId());
                byId.setBalance(byId.getBalance()+totalMoney);
                userService.updateById(byId);
            }
            earlyAction.setEarlyStart(earlyStart);
            earlyAction.setLuckStart(luckStart);
            earlyAction.setPowerStart(powerStart);
            earlyActionMapper.updateById(earlyAction);
        }
    }

    // 9:00 将早起打卡设置成已完结，并启动下一场打卡
    @Scheduled(cron = "0 0 9 * * ?",zone = "Asia/Shanghai")
    @Transactional
    public void yesterDayActionToEnd() {
        // 获取当前时间
        LocalDate nowDate = LocalDate.now();
        nowDate=nowDate.plusDays(-1);
        EarlyAction earlyAction = earlyActionMapper.selectActionByTime(nowDate.toString());
        if (earlyAction!=null){
            earlyAction.setActionStatus(ActionStatus.END);
            earlyActionMapper.updateById(earlyAction);
        }
        // 创建新的打卡会活动
        EarlyAction newEarlyAction = new EarlyAction();
        newEarlyAction.setActionStatus(ActionStatus.APPLYING);
        earlyActionMapper.insert(newEarlyAction);
    }

}
